package com.mk.cloud.pt.controller.app;

import act.Act;
import act.apidoc.Description;
import act.controller.Controller;
import com.mk.cloud.common.domain.FlApplyCash;
import com.mk.cloud.common.domain.FlApplyCashHistory;
import com.mk.cloud.common.domain.PUser;
import com.mk.cloud.common.enums.ConfigCode;
import com.mk.cloud.common.enums.ErrorCode;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.exception.UserException;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.header.HeaderService;
import com.mk.cloud.common.interceptor.header.IHLogin;
import com.mk.cloud.common.util.DateUtils;
import com.mk.cloud.common.util.ResUtils;
import com.mk.cloud.common.util.S2;
import com.mk.cloud.pt.service.app.RewardService;
import org.apache.commons.lang3.StringUtils;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.With;
import org.osgl.util.S;

import javax.inject.Inject;
import java.math.BigDecimal;
import java.util.Date;

@Controller("app/applycash")
public class FlApplyCashController extends HeaderService{

    @Inject
    private FlApplyCash.Mapper flApplyCashMapper;
    @Inject
    private FlApplyCashHistory.Mapper flApplyCashHistoryMapper;
    @Inject
    private PUser.Mapper pUserMapper;
    @Inject
    private RewardService rewardService;

    @PostAction("add.json")
    @Description("提现申请")
    @With({IHLogin.class, ITransacte.class})
    public String addApplyCash(FlApplyCash applyCash,String code,String timestamp){
        checkImgCode(code,timestamp);
        applyCash.setAcid(S2.buildOrderId("TX"));
        applyCash.setFuserid(me.getFuserid());
        applyCash.setFrealname(S.isNotBlank(me.getRealname())?me.getRealname():me.getNickname());
        applyCash.setAwfdate(DateUtils.getTimeNow());
        applyCash.setCoid(me.getCoid());
        applyCash.setAwfstate(GlobalValues.AWF_STATE_0.value());
        applyCash.setOrderno(new Date().getTime()+"");
        String wdfee = ConfigCode.valueOf("SERVICE_CHARGE").value(me.getCoid());
        applyCash.setWdfees(new BigDecimal(wdfee));
        applyCash.setRefamount(applyCash.getWdamount().subtract(applyCash.getWdfees()));
        flApplyCashMapper.insert(applyCash);
        pUserMapper.lockAmountByFuserid(me.getFuserid(),applyCash.getWdamount().add(applyCash.getWdfees()));
        addApplyCashHistory(applyCash,"用户申请提现");
        //增加展示流水
        PUser user = pUserMapper.single(me.getFuserid());
        rewardService.addFlow(user,user.getFuserid(),GlobalValues.FLOW_TYPE_1.value(),
                applyCash.getWdamount(),user.getWalletamount().subtract(user.getLockamount()),
                GlobalValues.SOURCETYPE_0.value(),"申请提现");
        return ResUtils.OK();
    }

    /**
     * 添加历史记录
     */
    @With({IHLogin.class, ITransacte.class})
    private void addApplyCashHistory(FlApplyCash applyCash,String description){
        FlApplyCashHistory history = new FlApplyCashHistory();
        history.setAchid(S2.UUID());
        history.setCoid(me.getCoid());
        history.setBuilddate(DateUtils.getTimeNow());
        history.setAfterstatus(applyCash.getAwfstate());
        history.setPaytype(GlobalValues.PAY_TYPE_2.value());
        history.setAcid(applyCash.getAcid());
        history.setDescription(description);
        flApplyCashHistoryMapper.insert(history);

    }

    /**
     * 校验图形验证码
     * @param code
     */
    private void checkImgCode(@Description("图形验证码") String code,
                              @Description("时间戳") String timestamp) {
        //region 图形验证码开始验证
        UserException.throwIf(StringUtils.isBlank(code), ErrorCode.E201014);
        String cachedCodeImg = Act.cache().get(timestamp);
        UserException.throwIf(!StringUtils.equalsIgnoreCase(cachedCodeImg,code),ErrorCode.E201009);
        //endregion
    }
}
